package org.infosys.org.controller;

import org.infosys.basic.exception.BusinessException;
import org.infosys.basic.utils.ExcelUtil;
import org.infosys.org.domain.ShopAuditLog;
import org.infosys.org.service.IShopService;
import org.infosys.org.domain.Shop;
import org.infosys.org.query.ShopQuery;
import org.infosys.basic.utils.AjaxResult;
import org.infosys.basic.utils.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    public IShopService shopService;


    /**
     * 保存和修改公用的
     * @param shop  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if( shop.getId()!=null)
                shopService.update(shop);
            else
                shopService.save(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("保存对象失败！"+e.getMessage());
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.remove(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setMsg("删除对象失败！"+e.getMessage());
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Shop get(@PathVariable("id")Long id)
    {
        return shopService.loadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Shop> list(){
        return shopService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<Shop> json(@RequestBody ShopQuery query)
    {
        return shopService.loadQuery(query);
    }

    /**
    * @Title: settlement
    * @Description: 店铺入驻
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/1 20:10
    * @Parameters: []
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/settlement")
    public AjaxResult settlement(@RequestBody Shop shop) {
        try {
            shopService.settlement(shop);
            return AjaxResult.me();
        } catch (BusinessException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙，请重试！").setSuccess(false);
        }
    }

    /**
    * @Title: exportExcel
    * @Description: 导出excel
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 10:20
    * @Parameters: [response]
    * @Return void
    */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response) {
        // 查询所有店铺的数据
        List<Shop> shops = shopService.loadAll();
        // 使用excel进行导出
        ExcelUtil.exportExcel(shops, null, "店铺信息", Shop.class, "店铺信息.xls", response);
    }

    /**
    * @Title: importExcel
    * @Description: 导入
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 10:44
    * @Parameters: [file]
    * @Return void
    */
    @PostMapping("/import/excel")
    public void importExcel(@RequestPart("file") MultipartFile file) {
        // 调用工具类进行导入
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shopService.batchSave(shops);
    }

    /**
    * @Title: loadShopEcharsData
    * @Description: 店铺状态分类数据报表
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 10:59
    * @Parameters: []
    * @Return java.util.Map<java.lang.String,java.lang.Object>
    */
    @GetMapping("/echarts")
    public Map<String, Object> loadShopEcharsData() {
        return shopService.loadShopEcharsData();
    }

    /**
    * @Title: pass
    * @Description: 店铺通过
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 15:39
    * @Parameters: []
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/audit/pass")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.pass(shopAuditLog);
            return AjaxResult.me();
        } catch (MessagingException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙").setSuccess(false);
        }
    }

    /**
    * @Title: audit
    * @Description: 邮箱链接激活
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 22:25
    * @Parameters: [id]
    * @Return java.lang.String
    */
    @PostMapping("/audit/activation/{id}")
    public void audit(@PathVariable("id")Long id) {
        shopService.audit(id);
    }

    /**
    * @Title: reject
    * @Description: 驳回
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 23:26
    * @Parameters: [shopAuditLog]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/audit/reject")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.reject(shopAuditLog);
            return AjaxResult.me();
        } catch (MessagingException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙").setSuccess(false);
        }
    }

    /**
    * @Title: refuse
    * @Description: 拒绝通过
    * @Author: hank
    * @Version: 1.0
    * @Date:  2022/7/3 23:27
    * @Parameters: [shopAuditLog]
    * @Return org.infosys.basic.utils.AjaxResult
    */
    @PostMapping("/audit/refuse")
    public AjaxResult refuse(@RequestBody ShopAuditLog shopAuditLog) {
        try {
            shopService.refuse(shopAuditLog);
            return AjaxResult.me();
        } catch (MessagingException e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg(e.getMessage()).setSuccess(false);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMsg("系统繁忙").setSuccess(false);
        }
    }
}
